import requests
import json
from Crypto.Cipher import AES
import base64
def s(cyphertext):
    key=b'cpcG!8%@8yS1!wBT'
    cyphertext=base64.decodebytes(cyphertext.encode('utf-8'))
    cipher = AES.new(key,mode=AES.MODE_ECB)
    text=cipher.decrypt(cyphertext).decode('utf-8')
    if not text[-1].isdigit():
        text=text[:-ord(text[-1])]
    return text

def o(url,text):
    num=str(text).split(',')
    num2=[]
    for i in range(len(num)-1,0,-2):
        num2.append([num[i-1],num[i]])
    for i in num2:
        a=int(i[0])
        b=int(i[1])
        url=url[0:a]+url[a+b:]
    return url
    
def photo2animate(p_url):
    url='https://pc.meitu.com/algorithm/andy'
    data={
        "parameter": {
            "rsp_media_type": "jpg",
            "cartoonType": 13,
            "type": 0,
            "filterType": 3001,
            "backgroundType": 3001,
            "makeupType": 7,
            "use3X": 0
        },
        "media_info_list": [
            {
                "media_data": p_url,
                "media_extra": {},
                "media_profiles": {
                    "media_data_type": "url"
                }
            }
        ]
    }
    js=requests.post(url,json=data).json()
    text=s(js['data']['key'])
    dat=o(js['data']['url'],text)
    content=base64.b64decode(dat)
    with open('E:/User/Desktop/base64picture.jpg','wb') as f:
        f.write(content)
    print("ok")
# p_url='https://gchat.qpic.cn/gchatpic_new/3124822772/1039987894-2464863783-2835B6C2570B38BEF15DD58DC130CC39/0?term=3'
url='https://img1.baidu.com/it/u=1458145492,2870391772&fm=253&fmt=auto&app=120&f=JPEG?w=1200&h=675'
photo2animate(url)
